package com.mmall.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CartMapper;
import com.mmall.dao.ProductMapper;
import com.mmall.pojo.Cart;
import com.mmall.pojo.Product;
import com.mmall.service.CartService;
import com.mmall.util.BigDecimalUtil;
import com.mmall.util.PropertiesUtil;
import com.mmall.vo.CartProductVo;

import com.mmall.vo.CartVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    public ServerResponse<CartVo> addProductToCart(Integer userId,Integer productId,Integer count){
          if(userId==null||productId==null){
              return  ServerResponse.createServerResponseByFailure("参数错误");
          }
        Cart cart=cartMapper.selectProductByUserIdAndProductId(userId,productId);
          if(cart==null){
              cart.setUserId(userId);
              cart.setProductId(productId);
              cart.setQuantity(count);
              cartMapper.insert(cart);
              return ServerResponse.createServerResponseBySuccess("添加购物车成功!");
          }
          else{
               cart.setQuantity(count);
               cartMapper.updateSelectiveByPrimaryKey(cart);
          }
          CartVo cartVo ;
          cartVo=this.getCartList(userId);

      return ServerResponse.createServerResponseBySuccess(cartVo);
    }

    public ServerResponse<CartVo> cartList(Integer userId){
        CartVo cartVo = this.getCartList(userId);
        return ServerResponse.createServerResponseBySuccess(cartVo);
    }

    public ServerResponse deleteCart(Integer userId,String  productIds){
        if(userId==null|| StringUtils.isBlank(productIds)){
            return  ServerResponse.createServerResponseByFailure("参数错误");
        }
        List<String> productIdList = Splitter.on(",").splitToList(productIds);

        int result = cartMapper.deleteByUserIdAndProductId(userId,productIdList);
        if(result==1){
            CartVo cartVo = this.getCartList(userId);
            return ServerResponse.createServerResponseBySuccess("移除成功");
        }
        else{
            return ServerResponse.createServerResponseByFailure("移除失败");
        }
    }

    public ServerResponse updateCheckedOneOrAll(Integer userId,Integer productId,Integer checked){

        if(userId==null||checked==null){
            return  ServerResponse.createServerResponseByFailure("参数错误");
        }
        int result= cartMapper.updateCheckedOneOrAll(userId,productId,checked);
        if(result==1){
            CartVo cartVo = this.getCartList(userId);
            return ServerResponse.createServerResponseBySuccess();
        }
        else{
            return ServerResponse.createServerResponseByFailure();
        }
    }

    //更新数量操作
    public ServerResponse<CartVo>updateCart(Integer userId,Integer productId,Integer count){
        if(userId==null||productId==null){
            return  ServerResponse.createServerResponseByFailure("参数错误");
        }
        Cart cart=cartMapper.selectProductByUserIdAndProductId(userId,productId);
        if(cart!=null){
              cart.setQuantity(count);
              cartMapper.updateSelectiveByPrimaryKey(cart);
          }
        CartVo cartVo = new CartVo();
        cartVo=this.getCartList(userId);
        return ServerResponse.createServerResponseBySuccess(cartVo);
    }
    public CartVo getCartList(Integer userId) {
       BigDecimal cartTotalPrice = new BigDecimal("0");
       CartVo cartVo = new CartVo();
       List<Cart>   cartList = cartMapper.selectCartByUserId(userId);//获取到购物车列表
       List<CartProductVo> cartProductVoList = Lists.newArrayList();
       CartProductVo cartProductVo;
       if (cartList.size() != 0) {
           for (Cart cart : cartList) {
               cartProductVo = new CartProductVo();
               cartProductVo.setId(cart.getId());
               // cartProductVo.setQuantity(cart.getQuantity());
               cartProductVo.setUserId(userId);
               Product product = productMapper.selectByPrimaryKey(cart.getProductId());
               if (product != null) {
                   cartProductVo.setProductName(product.getName());
                   cartProductVo.setProductPrice(product.getPrice());
                   cartProductVo.setSubTitle(product.getSubtitle());
                   cartProductVo.setMainImage(product.getMainImage());
                   cartProductVo.setProductStock(product.getStock());//
                   cartProductVo.setLimitQuantity(product.getStock());
                   int buylimitCategory;
                   if (cart.getQuantity() >= product.getStock()) {
                       buylimitCategory = cart.getQuantity();
                   } else {
                       buylimitCategory = product.getStock();
                       Cart cartTemp = new Cart();
                       cart.setId(cart.getId());
                       cart.setQuantity(buylimitCategory);
                       cartMapper.updateSelectiveByPrimaryKey(cartTemp);
                   }
                   cartProductVo.setQuantity(buylimitCategory);
                   cartProductVoList.add(cartProductVo);
                   //计算总价
                   cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartProductVo.getQuantity().doubleValue()));
                   cartProductVo.setProductChecked(cart.getChecked());//是否已勾选
                   if (cartProductVo.getProductChecked() == Const.Cart.CHECKED) {
                       cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(), cartProductVo.getProductTotalPrice().doubleValue());
                   }

               }
           }
       }
       cartVo.setCartProductVoList(cartProductVoList);
       cartVo.setCartTotalPrice(cartTotalPrice);
       cartVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
       //设置是否全选中
       cartVo.setAllCkecked(checkedAll(userId));
       return cartVo;
   }
    public boolean checkedAll(Integer userId){
        if(userId==null){
            return false;
        }
       return cartMapper.checkedAll(userId)==0;
   }

}
